home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / Char Set & String Stuff / CharSet Read Me < prev   
Text File  |  2000-10-10  |  9KB  |  128 lines

  1.  
  2.  
  3.       CharSet and String Stuff plugin 1.23
  4.  
  5.  
  6.   by Theo, <delete@softhome.net> <http://www.thsmith.dircon.co.uk/>
  7.  
  8. The demo RB project relies on TT's MemoryBlock plugin, from <http://www.tempel.org/rb/>
  9. The Read Me file needs Text Edit Plus, from <http://members.aol.com/tombb/> to see the styles in it.
  10.  
  11.  
  12. The plugin seems huge, (125k or so), but it is actually tiny on Macs. The PPC code is 8k, 68k code 6k, but the Win32 code is 100k. So you don't need to worry about bulking up your apps with it unless you are using it for Win32.
  13.  
  14.  
  15. The "CharSet and String Stuff" plugin does a few things.
  16.  
  17. Here are a list of the methods:
  18.  
  19. EmptyString(Length as Integer, CharVal as integer) as String
  20. FastInStrB(Start as Integer, Finish as integer, Container as String, Find as string) as Integer
  21. InByte(Start as Integer, Finish as integer, Container as String, Byte as Integer) as Integer
  22. OutCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer
  23. OutRevCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer
  24. InCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer
  25. MidAscB(Container as String, Offset as Integer) as Integer
  26. ByteOfString(Container as String, Offset as Integer) as Integer
  27. StringToPointer(TheString as String) as Integer
  28.  
  29. MemoryBlock Methods:
  30.     OutCharSet(Start as Integer, Finish as Integer, CharSet as MemoryBlock) as Integer
  31.     InCharSet(Start as Integer, Finish as Integer, CharSet as MemoryBlock, byref CharSetPos as Integer) as Integer
  32.     InBlock(Start as Integer, Finish as Integer, SearchPattern as MemoryBlock) as Integer
  33.     InSegBlock(Start as Integer, Finish as Integer, Search as MemoryBlock, SearchStart as integer, SearchEnd as Integer) as Integer
  34.     InRange(Start as Integer, Finish as Intger, RangeStart as Integer, RangeEnd as Integer) as Integer
  35.     InByte(Start as Integer, Finish as Integer, Byte as Integer) as Integer
  36.     InRevByte(Start as Integer, Finish as Integer, Byte as Integer) as Integer
  37.     SetSize(Size as Integer)
  38.  
  39.  
  40. How Parameters work:
  41.  
  42. The MemoryBlock versions are 0 based, and the string versions are 1 based for all position parameters and also for the result. This means character 2 of a string is the same as character 1 for a MemoryBlock. This is to keep in line with how RB works. If you pass -1 to a MemoryBlock Finish parameter or 0 to a string Finish parameter, the plugin interprets it as you wanting to go to the end of the string/memoryblock.
  43.  
  44. InCharSet returns the first character in the container that is contained itself inside the CharSet parameter. So InCharSet("xxxxxaxxxx", 1, 0, "a") would return 6, because the first character in "xxxxxaxxxx" that is in "a", is the 6th character. The byreffed parameter CharSetPos returns the position within the characterset of the byte that matched, saving you having to do a .InByte on the Character Set to find its position if you need to. If you don't need this parameter, just pass a dummy integer that you only ignore.
  45.  
  46. OutCharSet returns the first character in the container that *isn't* contained in the CharSet parameter. So OutCharSet("abcdefghijk",1,0,"abcdeghijk") would return 6 also, because "f" isn't in "abcdeghijk".
  47.  
  48. This has many uses, especially for parsing in general, where you might want to find where a certain item ends, eg: 
  49.  
  50. <html test>, <html test="something">, <html test anothervalue="something">.
  51.  
  52. Now there is an example that could be helped by doing character set searching on strings. If you wanted to see where attribute "test" ended and what was after it, you have several possibilities. It gets worse for parsing other languages or situations even in HTML. Also, this plugin does the searching very fast, while that kind of parsing can get very slow using other methods.
  53.  
  54. The parameters Start and Finish specify where in the string or MemoryBlock the searching starts and finishes. The string parameters and result is 1 bounded, the MemoryBlock version is 0 bounded. If you want the search to go to the end, pass "-1" to the Finish parameter for a MemoryBlock version, and 0 to Finish for the string version.
  55.  
  56. OutRevCharSet does the same as OutCharSet, except in reverse.
  57.  
  58. MidAscB is just like doing ascb(midb(mystring, Offset, 1)), except 9x faster!
  59.  
  60. ByteOfString is the same as MidAscB, but a little faster than MidAscB, and you need to pass 0 for the first character of to ByteOfString. Its a little faster because MidAscB needs to subtract 1 to get the byte, while ByteOfString doesn't.
  61.  
  62. EmptyString returns a string of the length passed, and the character type passed.
  63.  
  64. FastInstrB does the same as InstrB does, except 33% faster, and it gives you an end position parameter.
  65.  
  66. InByte this is like doing an InByte on a MemoryBlock, except this works on a string. It has advantages such as you don't need to create a string to do an InStrB with if you only have one char, and its a little faster (33%).
  67.  
  68. StringToPointer returns the location in the memory that a string's first character is. This means you can create a MemoryBlock that points to that string, using
  69.  
  70.  
  71. Function StringToMemoryBlock(s as string) As MemoryBlock
  72.    dim n as integer, mb, resultMB as MemoryBlock
  73.    n = lenb(s)
  74.    if n > 0 then
  75.       mb = newMemoryBlock(4)
  76.       mb.long(0) = StringToPointer(s)
  77.       resultMB = mb.ptr(0)
  78.       resultMB.setSize n
  79.       return resultMB
  80.    end if
  81. End Function
  82.  
  83. Doing this actually doesn't duplicate all the data in a string, which allows you both to alter a string IN PLACE!!!! (and do this fast), and also it allows you to do your MemoryBlock methods on large strings (200k for example) without wasting a huge load of RAM! WONDERFUL!
  84.  
  85.  
  86. InBlock This searches the MemoryBlock for the SearchPattern parameter MemoryBlock, just like InStr does except on MemoryBlocks. This method has a finish parameter unlike InStr.
  87.  
  88. InSegBlock The search method you have been waiting for!!!!! This one allows you to search within a range (Start and Finish) of the container, and only match a range of the search pattern! This is very useful in some string functions, meaning that you no longer need to create new MemoryBlock of the right size and choosing the right text to copy into the MemoryBlock to do searches with using InBlock. You can simply search the container for a segment of the pattern! Also allows you to choose the direction of searching. True is forward, false backward.
  89.  
  90. InByte Searches the container MemoryBlock for the byte passed.
  91.  
  92. InRevByte Searches the container MemoryBlock for the byte passed, backwards.
  93.  
  94. InRange Searches from the start to the finish position for any bytes that aren't in the range passed, this is useful for finding if a file contains high ascii or other things like that, and fast.
  95.  
  96. SetSize just sets the size of the MemoryBlock.
  97.  
  98. If you have any ideas of string or MemoryBlock functions that you might need, email me the idea and I might make it for you.
  99.  
  100. The StringToPointer function does something that RB wasn't planned to be able to do, it is an unlikely possibility that RS may change some internal workings someday, and StringToPointer will not work like it should. It works fine as of RB 3.0a5 however.
  101.  
  102.  
  103. The source code is in CodeWarrior 5.3 and can be got from my website. You will also need the "RB DLL Plugin Panel" and "RB DLL Plugin PostLinker", which you can get from REALsoftware's website. <http://www.realbasic.com/>
  104.  
  105.  
  106. Version History:
  107.  
  108.  
  109. 1.0 - First release.
  110.  
  111. 1.01 - Fixed start and finish parameter bugs in most of the methods.
  112. Added FastInStrB.
  113.  
  114. 1.1 - Added .InSegBlock to MemoryBlock
  115. Added .InRevByte
  116. Made tiny code improvements all over.
  117. Fixed a parameter bug in FastInStrB.
  118.  
  119. 1.11 - Added byref CharSetPos to the parameters of .InCharSet
  120. Fixed a bug in ByteOfString and MidAscB
  121.  
  122. 1.2 Added InByte for strings, and changed all chars to unsigned chars in the source, giving another big speed up! Its now no longer slower than RB's InStrB in any cases! Before it was if you had very long pattern strings (200 bytes or so).
  123.  
  124. 1.201 Fixed a documentation mistake with InCharSet
  125.  
  126. 1.21 Added OutRevCharSet. Simply because I happened to need it for a project of mine. I haven't added Rev equivilents of the rest because I haven't needed them myself yet (and I've had no requests for it either).
  127.  
  128. 1.23 Totally redid the core code, its now just as fast as before, but now the code is much more readable, and about 70x more adaptable :o) and it results in smaller code too. Added InRange also.